package com.turing.post.tuxedo.service;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.cardtransactions.util.CardTransBusiness;
import com.turing.post.common.util.SysOpUser;
import com.turing.post.common.util.SysOpUserDao;
import com.turing.post.transaction.util.MrchBusiness;
import com.turing.post.tuxedo.util.IChangePassword;
import com.turing.post.tuxedo.util.LocalService;
import com.turing.post.tuxedo.util.LocalServiceImpl;
import com.turing.post.tuxedo.util.OChangePassword;
import com.turing.post.tuxedo.util.PCPSService;
import com.turing.post.tuxedo.util.TransRequest;
import com.turing.post.tuxedo.util.TransResponse;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

@Stateless(mappedName = "PCPS.SVC9002")
@Remote({ LocalService.class, PCPSService.class })
@Local
public class ChangePassword extends LocalServiceImpl implements LocalService,
		PCPSService {

	@EJB(mappedName = "cardtransactions.impl.CardTransBusinessImpl")
	private CardTransBusiness cardTransBusinessImpl;

	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
	private SysOpUserDao sysOpUserDaoImpl;

	@EJB(mappedName = "transaction.impl.MrchBusinessImpl")
	private MrchBusiness mrchBusinessImpl;

	/**
	 * generate IChangePassword
	 * 
	 * @return IChangePassword
	 */
	public IChangePassword makeRequestObject() {
		return new IChangePassword();
	}

	/**
	 * ChangePassword 数据检查
	 * 
	 * @param req
	 * @param ack
	 */
	protected void preService(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {
	}

	/**
	 * ChangePassword service entry 预付卡更改卡密码的业务逻辑
	 * 
	 * @param req
	 * @param ack
	 */
	protected void Service(TransRequest req, TransResponse ack)
			throws PCPSApplicationException {

		IChangePassword itr = (IChangePassword) req;
		OChangePassword otr = (OChangePassword) ack;

		PCPSUtil.Log("CardNo:" + itr.cardObj.cardNo);

		// 验证卡信息的完整性
		itr.cardObj.cardNo = mrchBusinessImpl.getValidateCardNo(req.merchantNo,
				itr.cardObj.cardNo, itr.cardObj.track2, itr.cardObj.track3);

		SysOpUser sou = sysOpUserDaoImpl
				.getInternetAgentSysOpUserByIssueCompany(req.merchantNo
						.substring(0, 6));

		cardTransBusinessImpl.ChangePassword(itr.cardObj.cardNo, itr.passwd,
				itr.newPwd, sou.getOperId(), itr.merchantNo);

		// 成功返回
		otr.respCode = TransResponse.RESPCODE_SUCC;
	}

	/**
	 * ChangePassword 服务入口
	 * 
	 * @param req
	 * @return TransResponse
	 */
	public TransResponse Service(TransRequest req)
			throws PCPSApplicationException {
		// 按普通方法执行服务
		return CommonService(req, new OChangePassword());
	}
}